﻿using Models;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Scheduledscheduling
{
    public partial class frm_TaskLstLog : Form
    {
        TaskOptions task;
        public frm_TaskLstLog(TaskOptions task)
        {
            InitializeComponent();
            this.StartPosition = FormStartPosition.CenterScreen;
            this.ControlBox = true;

            this.MaximizeBox = false;
            this.task = task;
            this.Load += new EventHandler(frm_load);
            this.Text = task.GroupName;
            dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells;
            dataGridView1.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.AllCells;
            dataGridView1.AllowUserToAddRows = false;
            dataGridView1.ReadOnly = true;
        }

        private void frm_load(object sender, EventArgs e)
        {
            var dt = Sugar.db.Queryable<TaskLog>()
                .Where(it => it.TaskName == task.TaskName && it.GroupName == task.GroupName)
                .OrderByDescending(x => x.Id)
                .Take(200)
                .ToDataTable();

            var lst = new List<TaskLogShow>();
            int no = 1;
            foreach (DataRow dr in dt.Rows)
            {
                var logShow = new TaskLogShow
                {
                    No = (no++).ToString(),
                    Id = dr["Id"] != DBNull.Value ? Convert.ToInt64(dr["Id"]) : 0,
                    GroupName = dr["GroupName"]?.ToString(),
                    TaskName = dr["TaskName"]?.ToString(),
                    BeginDate = dr["BeginDate"]?.ToString(),
                    EndDate = dr["EndDate"]?.ToString(),
                    Msg = dr["Msg"]?.ToString()
                };
                lst.Add(logShow);
            }

            dataGridView1.DataSource = lst;
        }
    }
}
